#include "DockWidgetTitle.h"
#include <QPainter>
#include <QStyle>
#include <QPoint>
#include <QDockWidget>
#include <QMouseEvent>

DockWidgetTitle::DockWidgetTitle()
{

}



DockWidgetTitle::DockWidgetTitle(QString dockName,QWidget *parent)
: QWidget(parent)
{
    //pixMap
    minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
    closePix  = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
    floatPix = style()->standardPixmap(QStyle::SP_TitleBarNormalButton);
    name = dockName;
}



//set TitleBar minimumSize
QSize DockWidgetTitle::minimumSizeHint() const
{
    return QSize(156, 24);
}



//绘制titleBar
void DockWidgetTitle::paintEvent(QPaintEvent*)
{
    QPainter painter(this);
    QRect rect = this->rect();
    width = rect.width();
    //drawPixmap
    painter.setPen(QPen(QColor(185, 185, 185), 2, Qt::SolidLine));
    painter.setBrush(QBrush(QColor(218, 218, 218), Qt::SolidPattern));
    painter.drawRect(rect.left(), rect.top(), rect.width(), rect.height());
    painter.drawPixmap(rect.topRight() - QPoint(closePix.width() + 10, -6), closePix);
    painter.drawPixmap(rect.topRight() - QPoint(floatPix.width() + 10 + closePix.width() + 10, -6), floatPix);
    painter.drawPixmap(rect.topRight() - QPoint(floatPix.width() + 10 + minPix.width() + 10
                                                + closePix.width() + 10, -3), minPix);
    //drawText
    painter.setPen(QPen(Qt::black, 1, Qt::SolidLine));
    painter.setBrush(Qt::NoBrush);
    painter.drawText(rect.left() + 10, rect.top()-2, rect.width(), rect.height(), Qt::AlignVCenter, name);

    //draw   Blank area
    painter.setPen(Qt::NoPen);
    painter.setBrush(QBrush(QColor(240, 240, 240), Qt::SolidPattern));//设置画刷形式
    painter.drawRect(rect.left(), rect.top() + 19, rect.width(), 5);

    painter.setRenderHint(QPainter::Antialiasing, true);

        // 绘制图标
    //painter.setPen(QPen(Qt::black, 1, Qt::SolidLine));
    //painter.setBrush(Qt::NoBrush);
    //painter.drawPixmap(rect.left() + 70,rect.top() + 2,rect.width() - 280, rect.height() - 8, QPixmap(":/resource/image/brower.png"));

}



void DockWidgetTitle::mousePressEvent(QMouseEvent *event)
{
    QPoint pos = event->pos();

    QRect rect = this->rect();

    QDockWidget *dw = qobject_cast<QDockWidget*>(parentWidget());
    Q_ASSERT(dw != 0);

    //in this case,pix width = 10,pix spacing = 10
    int right = rect.right() - pos.x();
    QWidget *widget = dw->widget();
    if (right >= 10 && right <= 20)
    {
        event->accept();
        dw->close();
    }
    else if (right >= 30 && right <= 40){
        event->accept();
        if (widget->isVisible()){

        }
        else {
            widget->show();
            //dw setMinimumWidth initialize
            dw->setMinimumWidth(156);
        }
    }
    else if (right >= 50 && right <= 60){
        event->accept();
        if (widget->isVisible()){
            //click minPix ,dw setMinimumWidth
            dw->setMinimumWidth(width);
            widget->hide();
        }
        else {

        }
    }
}
